﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Site3AI.Models;

namespace Site3AI.Controllers
{
    [Authorize]
    public class KBController : _BaseController
    {
        public ActionResult Index()
        {
            return View(DB.CmsItems.AsEnumerable());            
        }
        public ActionResult Preview(string id)
        {
            var m = DB.CmsItems.Find(id);
            return View(m);
        }
        [HttpGet]
        public ActionResult Edit(string id)
        {
            ViewBag.CmsCatalogs = DB.CmsCatalogs.AsEnumerable();
            var m = string.IsNullOrEmpty(id) ? new CmsItem() : DB.CmsItems.Find(id);
            return View(m);
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(CmsItem tm)
        {
            
            var m = string.IsNullOrEmpty(tm.ID)?tm: DB.CmsItems.Find(tm.ID);

            if (string.IsNullOrEmpty(m.ID))
            {
                m.ID = DB.NewID();
                DB.CmsItems.Add(m);
            }
            else {
                DB.Entry(m).CurrentValues.SetValues(tm);
            }
            DB.SaveChanges();
            return RedirectToAction("Preview", new { id = m.ID });
            
        }

        public ActionResult Delete(string id)
        {
            var m = DB.CmsItems.Find(id);
            DB.CmsItems.Remove(m);
            DB.SaveChanges();

            return RedirectToAction("Index");
        }

        public ActionResult ListCata()
        {
            return View(DB.CmsCatalogs.AsEnumerable());
        }

        [HttpGet]
        public ActionResult EditCata(string id)
        {
            ViewBag.Hospitals = DB.Hospitals.AsEnumerable();
            ViewBag.CmsCatalogs = DB.CmsCatalogs.AsEnumerable();
            var m = string.IsNullOrEmpty(id) ? new CmsCatalog() { PID="0"} : DB.CmsCatalogs.Find(id);
            return View(m);
        }

        [HttpPost]
        public ActionResult EditCata(CmsCatalog tm,string Hospital)
        {
            ViewBag.Hospitals = DB.Hospitals.AsEnumerable();
            ViewBag.CmsCatalogs = DB.CmsCatalogs.AsEnumerable();
            var m = string.IsNullOrEmpty(tm.ID) ? tm : DB.CmsCatalogs.Find(tm.ID);

            if (string.IsNullOrEmpty(m.ID))
            {
                m.ID = DB.NewID();
                DB.CmsCatalogs.Add(m);
            }
            else
            {
                DB.Entry(m).CurrentValues.SetValues(tm);
            }
            DB.SaveChanges();

            var hs = m.Hospitals.ToList();
            bool findHosp = false;
            foreach (var h in hs)
            {
                if (h.HospitalID == Hospital){
                    findHosp = true; 
                }else{
                    m.Hospitals.Remove(h);
                }
            }
            if (!findHosp) { m.Hospitals.Add(DB.Hospitals.Find(Hospital)); }

            DB.SaveChanges();
            
            return View(m);

        }

        public ActionResult DeleteCata(string id)
        {
            var m = DB.CmsCatalogs.Find(id);
            DB.CmsCatalogs.Remove(m);
            DB.SaveChanges();

            return RedirectToAction("ListCata");
        }

    }
}
